AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize()
    self.Entity:PhysicsInit( SOLID_VPHYSICS )
    self.Entity:SetMoveType(  MOVETYPE_VPHYSICS )   
    self.Entity:SetSolid( SOLID_VPHYSICS )
	self.SpawnPos = self.Entity:GetPos()
	self.FuseLeft = 1200
	self.FuseMax = 1200
    self.PhysObj = self.Entity:GetPhysicsObject()
    if (self.PhysObj:IsValid()) then
		self.PhysObj:EnableGravity( true )
		self.PhysObj:EnableDrag( false ) 
        self.PhysObj:Wake()
		self.PhysObj:SetVelocity(Vector(self.Entity:BoundingRadius() * math.random(-30, 30), self.Entity:BoundingRadius() * math.random(-30, 30), self.Entity:BoundingRadius() * 30))
		self.PhysObj:AddAngleVelocity(VectorRand() * self.Entity:BoundingRadius() * 2)
    end
	self.Entity:EmitSound( "ambient/explosions/explode_"..math.random(1,4)..".wav", 100, math.Clamp( math.Clamp( (self.Entity:BoundingRadius() * 10), 1, 5000 ) * -1 + 255 + math.random(-5, 5), 50, 255 )  )

	local effect = EffectData()
	effect:SetEntity( self.Owner )
	effect:SetStart( self.Entity:GetPos() )
	effect:SetAttachment( 1 )
	effect:SetOrigin( self.Entity:GetPos() )
	effect:SetScale( self.Entity:BoundingRadius() )
	effect:SetMagnitude( 1 )
	util.Effect( "Explosive_Explosion", effect )	
	util.Effect( "Explosive_Ring", effect )	
	
end
 
 
function ENT:Think() 
	if (self.FuseLeft >= 1) then
		self.FuseLeft = self.FuseLeft - 1
	else
	
	end
	local alpha = (self.FuseLeft*255)/self.FuseMax
	self.Entity:SetColor(255,150,150,alpha)
	self.Entity:NextThink( CurTime() + 0.01)
	local remo = self:GetNWInt("ShouldBeRemoved")
	if(remo == 1)then
		self.Entity:Remove()
	end
	return true
end

function ENT:Explosion()
	local effect = EffectData()
	effect:SetEntity( self.Owner )
	effect:SetStart( self.Entity:GetPos() )
	effect:SetAttachment( 1 )
	effect:SetOrigin( self.Entity:GetPos() )
	effect:SetScale( self.Entity:BoundingRadius() )
	effect:SetMagnitude( 1 )
	util.Effect( "Explosive_Explosion", effect )
	util.Effect( "Explosive_Ring", effect )
	self.shakeeffect = ents.Create("env_shake")
	self.shakeeffect:SetKeyValue("amplitude", 16)
	self.shakeeffect:SetKeyValue("spawnflags", 4 + 8 + 16)
	self.shakeeffect:SetKeyValue("frequency", 200.0)
	self.shakeeffect:SetKeyValue("duration", 2)
	self.shakeeffect:SetKeyValue("radius", self.Entity:BoundingRadius() * 5)
	self.shakeeffect:SetPos(self.Entity:GetPos())
	self.shakeeffect:Fire("StartShake","",0)
	self.shakeeffect:Fire("Kill","",4)
end

function ENT:PhysicsUpdate()
return true
end

function ENT:PhysicsCollide( data, physobj )
	if(self.FuseLeft < 1150)then
		--util.Decal("Scorch", data.HitPos + data.HitNormal , data.HitPos - data.HitNormal) 
		self.Entity:EmitSound( "ambient/explosions/explode_"..math.random(1,4)..".wav", 100, math.Clamp( math.Clamp( (self.Entity:BoundingRadius() * 10), 1, 5000 ) * -1 + 255 + math.random(-5, 5), 50, 255 )  )
		self:Explosion()
		--util.BlastDamage( self.Entity, self.Entity, self.Entity:GetPos(), math.Clamp(self.Entity:BoundingRadius() * 5, 0, 500), self.Entity:BoundingRadius() * 5 )
		self.Entity:Remove()
	end	
end


function ENT:OnRemove()
end

function ENT:StartTouch()
end

function ENT:Touch()
end
 
function ENT:EndTouch()
end

function ENT:OnTakeDamage() 
end
